//
//  TrajectoreyViewController.m
//  carcareIOS
//
//  Created by wr on 15/6/25.
//  Copyright (c) 2015年 baozun. All rights reserved.
//

#import "TrajectoryViewController.h"
#import "OneWayMgr.h"
#import "MapLocAddressView.h"
#import "TrajectFontColorView.h"

#define HEADVIEW_HEIGHT (59)

#define SPEED_VIEW_WIDTH 56
#define SPEED_VIEW_HEIGHT 25.5
#define HIDDEN_BTN_Y 16
#define HIDDEN_BTN_H 32

@interface TrajectoryViewController ()

@property (nonatomic, strong) MapTrajectory *mapTrajectory;

@property (nonatomic , strong) OneDriveModel * model;
@property (nonatomic) BOOL outAndInBool;
@property (nonatomic , strong) UIView * headV;
@property (nonatomic , strong) MapLocAddressView * mapLocV;

@property (nonatomic , strong) TrajectFontColorView * trajectFontV;

@property (nonatomic , strong) UIButton * hiddenBtn;//是否隐藏上面悬浮的颜色说明view
@property (nonatomic , strong) UIButton * locMapCenterBtn;//定位到地图中心的按钮
@property (nonatomic ) BOOL trajectVOutAndInB;

@end

@implementation TrajectoryViewController
- (instancetype)initWithDataModel:(OneDriveModel *)model
{
    self = [super init];
    if (self) {
        self.model = model;
        self.mapTrajectory = [[MapTrajectory alloc] initWithType:Map_Type_CanTouch withModel:model];
//        self.mapTrajectory.delegate = self;

    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"车辆轨迹";
    
    [self navSetupBackItemWithIdentification:BACK_ICON_TITLE];
    
    
    [self createView];
    
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self.mapTrajectory reloadPolyLine];
}

- (void)createView
{
    UIImage * locImage = ImageNamed(@"loc_show");
    
    self.outAndInBool = YES;
    self.mapTrajectory.mapView.frame = CGRectMake(0, 0, __MAIN_WIDTH, self.view.bounds.size.height );
    __weak __typeof(self) wself = self;
    self.mapTrajectory.mapDidTouch = ^()
    {
        [UIView animateWithDuration:0.4 animations:^{
            [wself.headV setTop:(wself.outAndInBool ? - HEADVIEW_HEIGHT : 0)];
            [wself.mapLocV setTop:(wself.outAndInBool ? __MAIN_HEIGHT_64 : (__MAIN_HEIGHT_64 - MapFootLocV_HEIGHT))];
            [wself.hiddenBtn setTop:HIDDEN_BTN_Y + wself.headV.bottom];
            [wself.trajectFontV setTop:HIDDEN_BTN_Y + wself.headV.bottom];
            [wself.locMapCenterBtn setTop:wself.mapLocV.top - HIDDEN_BTN_Y - locImage.size.height];
        }];
        wself.outAndInBool = ! wself.outAndInBool;
    };
    [self.view addSubview:self.mapTrajectory.mapView];
    
    
    self.headV = [[[UIView viewWithFrame:CGRectMake(0, 0, __MAIN_WIDTH, HEADVIEW_HEIGHT)] setupOnView:self.view] resetBackgroundColor:[UIColor whiteColor]];
    [self.view bringSubviewToFront:self.headV];
    UIView * headLeftV = [self createSmallCount:self.model.speedUpTimes withTitle:@"急加速"];
    [headLeftV setCenterX:__MAIN_WIDTH / 4];
    
    UIView * rightV = [self createSmallCount:self.model.speedDownTimes withTitle:@"急减速"];
    [rightV setCenterX:__MAIN_WIDTH / 4 * 3];
    
    [[[UIView viewWithFrame:CGRectMake(__MAIN_WIDTH / 2, 10, 0.5, HEADVIEW_HEIGHT - 10 * 2)] resetBackgroundColor:[Theme lineNewColor]] setupOnView:self.headV];
    [[[UIView viewWithFrame:CGRectMake(0, HEADVIEW_HEIGHT - 0.5, __MAIN_WIDTH, 0.5)] resetBackgroundColor:[Theme lineNewColor]] setupOnView:self.headV];
    
    //起终点位置
    self.mapLocV = [[MapLocAddressView alloc]initWithFrame:CGRectMake(0, __MAIN_HEIGHT_64 - MapFootLocV_HEIGHT, __MAIN_WIDTH, MapFootLocV_HEIGHT)];
    [self.mapLocV reloadStarLoc:self.model.startPlace withEndLoc:self.model.endPlace];
    [self.view addSubview:self.mapLocV];
    
    self.trajectVOutAndInB = NO;
    self.trajectFontV = [[TrajectFontColorView alloc]initWithFrame:CGRectMake(__MAIN_WIDTH - TraV_Width - HIDDEN_BTN_Y - HIDDEN_BTN_H - TrangleRightX, HIDDEN_BTN_Y + self.headV.bottom, TraV_Width, TraV_Height)];
    [self.trajectFontV reloadViewWithTitleArr:@[@"0～20km/h",@"20～40km/h",@"40～60km/h",@"60km/h以上"] withColorArr:@[COLOR_HEXSTRING(@"#f74342"),COLOR_HEXSTRING(@"#ffc21c"),COLOR_HEXSTRING(@"#5fcc37"),COLOR_HEXSTRING(@"#16a0f1")]];
    [self.view addSubview:self.trajectFontV];
    [self.trajectFontV setHidden:!self.trajectVOutAndInB];
    
    //？的按钮
    self.hiddenBtn = [[UIButton buttonWithFrame:CGRectMake(__MAIN_WIDTH  - HIDDEN_BTN_Y - HIDDEN_BTN_H, HIDDEN_BTN_Y + self.headV.bottom, HIDDEN_BTN_H, HIDDEN_BTN_H) normalImage:ImageNamed(@"hiddenBtnQuestionImage") click:^{
        wself.trajectVOutAndInB = !wself.trajectVOutAndInB;
        
        [UIView animateWithDuration:0.3 animations:^{
            [wself.trajectFontV setHidden:!wself.trajectVOutAndInB];
            [wself.trajectFontV setTransform:(wself.trajectVOutAndInB ? CGAffineTransformMakeScale(1, 1) : CGAffineTransformMakeScale(0.01, 0.01))];
            
            [wself.trajectFontV setCenter:(wself.trajectVOutAndInB ? CGPointMake(__MAIN_WIDTH - TraV_Width / 2 - HIDDEN_BTN_Y - HIDDEN_BTN_H - TrangleRightX, HIDDEN_BTN_Y + wself.headV.bottom + TraV_Height / 2) : CGPointMake(__MAIN_WIDTH  - HIDDEN_BTN_Y - HIDDEN_BTN_H / 2, HIDDEN_BTN_Y + wself.headV.bottom + HIDDEN_BTN_H / 2))];
            
        }];
        [wself.hiddenBtn setImage:(wself.trajectVOutAndInB ? ImageNamed(@"hiddenBtnImage") : ImageNamed(@"hiddenBtnQuestionImage")) forState:UIControlStateNormal];
    }] setupOnView:self.view];
    
    //定位到地图中心的按钮
    self.locMapCenterBtn = [[UIButton buttonWithFrame:CGRectMake(__MAIN_WIDTH -  self.hiddenBtn.right, self.mapLocV.top - HIDDEN_BTN_Y - locImage.size.height, locImage.size.width, locImage.size.height) normalImage:locImage click:^{
        [wself.mapTrajectory showLocation];
    }] setupOnView:self.view];
    
}
- (UIView *)createSmallCount:(NSInteger)count withTitle:(NSString *)str
{
    UIView * view = [[UIView viewWithFrame:CGRectMake(0, 0, __MAIN_WIDTH / 2, HEADVIEW_HEIGHT)] setupOnView:self.headV];
    CGFloat labelH = 22;
    CGFloat labelY = 7;
    [[[UILabel labelWithFrame:CGRectMake(0, labelY, view.width, labelH) font:FONT_DEFAULT_Light(22) text:STRING_INT(count) textColor:COLOR_HEXSTRING(@"#454545")] setupOnView:view] setTextAlignment:NSTextAlignmentCenter];
    [[[UILabel labelWithFrame:CGRectMake(0, view.height - labelY - labelH, view.width, labelH) font:FONT_DEFAULT_Light(13) text:str textColor:COLOR_HEXSTRING(@"#7b7b7b")] setupOnView:view] setTextAlignment:NSTextAlignmentCenter];
    
    return view;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
